298B - Sail - CodeForces Solution


brute force greedy implementation *1200

Please click on ads to support us..

Python Code:

a = list(map(int, input().split()))[1:]
s = input()

x = a[2] - a[0]
y = a[3] - a[1]

if x>0: cx = 'E'
else: cx = 'W'

if y>0: cy = 'N'
else: cy = 'S'


x = abs(x)
y = abs(y)

for i in range(len(s)):

    if s[i] == cx:
        x-=1
    if s[i] == cy:
        y-=1

    if x<=0 and y<=0:
        print(i+1)
        exit()

print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,j,k=0,l=0,x,y,sx,sy,ex,ey,e=0,so=0,w=0,no=0,ans=0;
    cin>>n;
    cin>>sx>>sy>>ex>>ey;
    string s;
    cin>>s;
    x=sx-ex;
    y=sy-ey;
    if(x<0)
    {
        e=(x);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='E')
                e++;
            if(e==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    else if(x>0)
    {
        w=(x);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='W')
                w--;
            if(w==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    if(y<0)
    {
        no=(y);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='N')
                no++;
            if(no==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    else if(y>0)
    {
        so=(y);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='S')
                so--;
            if(so==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    if(so==0 && no==0 && w==0 && e==0)
    {
        cout<<ans+1<<endl;
    }
    else
        cout<<"-1"<<endl;
}


Comments

Submit
0 Comments
More Questions

1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map
630I - Parking Lot
160B - Unlucky Ticket
371B - Fox Dividing Cheese
584B - Kolya and Tanya
137B - Permutation
550C - Divisibility by Eight
5A - Chat Servers Outgoing Traffic
615A - Bulbs
5B - Center Alignment
549A - Face Detection
535B - Tavas and SaDDas
722C - Destroying Array